//
//  SecondViewController.m
//  QQ13
//
//  Created by KM on 16/10/13.
//  Copyright © 2016年 KM. All rights reserved.
//

#import "SecondViewController.h"
#import "SearchController.h"
#import "LoginData.h"
#import "DataCache.h"
#import "ListData.h"

@interface SecondViewController ()
@property (weak, nonatomic) IBOutlet UIButton *denglu;
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *password;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIToolbar *tool = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(onClose:)];
    UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    tool.items = @[space,item1];
    self.userName.inputAccessoryView = tool;
    self.password.inputAccessoryView = tool;

}
- (IBAction)chacel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)onClose:(id)sender
{
    [self.view endEditing:YES];
}


- (IBAction)OnClik:(id)sender {
    
//    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/login.php?name=%@&password=%@",self.userName.text,self.password.text]];
//    NSURLRequest *req = [NSURLRequest requestWithURL:url];
//    NSURLSession *session = [NSURLSession sharedSession];
//    NSURLSessionDataTask *task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
//    {
//        NSString *text = [[NSString alloc]initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
//        NSLog(@"%@",text);
//        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//        LoginData *d = [[LoginData alloc]initWithDictionary:dict];
//        dispatch_async(dispatch_get_main_queue(), ^{
//            // 刷新界面代码
//            
//            if (d.errcode == 0)    //[dict[@"errcode"] intValue] == 0
//            {
//                SearchController *sea = [self.storyboard instantiateViewControllerWithIdentifier:@"chat"];
//                [self.navigationController pushViewController:sea animated:YES];
//            }
//            else{
//                NSLog(@"%@",d.errinfo);
//            }
//        });
//    }];
//    
//    [task resume];
    
    [DataCache loginWithName:self.userName.text Password:self.password.text complete:^(LoginData *d) {
        if (d.errcode == 0) {
            SearchController *sea = [self.storyboard instantiateViewControllerWithIdentifier:@"chat"];
            [self.navigationController pushViewController:sea animated:YES];
        }
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
